//
//  CPCoursePakageUnzipAndDecryptTools.m
//  DCDownloadOpration
//
//  Created by collegepre on 16/8/11.
//  Copyright © 2016年 Wade. All rights reserved.
//

#import "CPCoursePakageUnzipAndDecryptTools.h"
#import "CPDownloadInfo.h"
#import <SSZipArchive/SSZipArchive.h>
#import "CPCourseZipSecurityInfo.h"
#import "NSString+DCDownload.h"
#import "CPMediaDecryptTool.h"
#import "CPSecurityTools.h"
#import "DCDownload.h"

@implementation CPCoursePakageUnzipAndDecryptTools
/**
 *  下载完成后解压/解密
 *
 *  @param downloadInfo 下载信息
 */
+ (void)unzipWithzipdownloadInfo:(CPDownloadInfo *)downloadInfo unzipProgress:(CPDownloadUnzipProgress)unzipProgress decryptProgress:(CPDownloadUnzipProgress)decryptProgress{
    
    __block NSFileManager *manager = [NSFileManager defaultManager];
    NSString *zipPath =downloadInfo.destinationPath;
    NSString * unzipPath = downloadInfo.unzipPath;
    if (![manager fileExistsAtPath:zipPath]) {
        unzipProgress(-1.0f);
        decryptProgress(-1.0f);
    }
    if (![manager fileExistsAtPath:downloadInfo.unzipPath]) {
         [manager createDirectoryAtPath:unzipPath withIntermediateDirectories:YES attributes:nil error:nil];
    }else{
        [manager removeItemAtPath:downloadInfo.unzipPath error:nil];
        [manager createDirectoryAtPath:unzipPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    if (downloadInfo.courseSecurityInfo.md5.isNotEmpty) {
        NSString *fileMD5 = [NSString fileMD5withFilePath:zipPath readingDataLength:CPFileHashDefaultChunkSize];
        if ([fileMD5 isEqualToString:downloadInfo.courseSecurityInfo.md5]) { // 比对Md5值
            [self unzipWithCoursePakageDownloadInfo:downloadInfo unzipProgress:^(float pro) {
                unzipProgress(pro);
            } decryptProgress:^(float pro) {
                decryptProgress(pro);
            }];
        }else{
            unzipProgress(-1.0f);
             decryptProgress(-1.0f);
            
            // 移除MD5值验证不通过的课件包
            [manager removeItemAtPath:unzipPath error:nil];
            [manager removeItemAtPath:zipPath error:nil];
        }
    }else{ // 如果md5值为空
        [self unzipWithCoursePakageDownloadInfo:downloadInfo unzipProgress:^(float pro) {
            unzipProgress(pro);
        } decryptProgress:^(float pro) {
            decryptProgress(pro);
        }];
    }
}

+ (void)unzipWithCoursePakageDownloadInfo:(CPDownloadInfo *)downloadInfo unzipProgress:(CPDownloadUnzipProgress)unzipProgress decryptProgress:(CPDownloadUnzipProgress)decryptProgress{
    NSFileManager *mgr = [NSFileManager defaultManager];
    NSString *zipPath = downloadInfo.destinationPath;
    NSString * unzipPath = downloadInfo.unzipPath;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [SSZipArchive unzipFileAtPath:zipPath toDestination:unzipPath progressHandler:^(NSString *entry, unz_file_info zipInfo, long entryNumber, long total) {
            float progress = entryNumber / (float)total ;
            //  解压进度进度回调
            unzipProgress(progress);
            
        } completionHandler:^(NSString *path, BOOL succeeded, NSError *error) {
            //  解压进度进度回调
            if (!succeeded) {
                unzipProgress(-1.0f);
                [mgr removeItemAtPath:zipPath error:nil];
                [mgr removeItemAtPath:unzipPath error:nil];
                
            }else
            {
                unzipProgress(1.0f);
                [mgr removeItemAtPath:zipPath error:nil];
                if (downloadInfo.courseSecurityInfo.header.isNotEmpty && downloadInfo.courseSecurityInfo.key.isNotEmpty)
                {
                    [[CPMediaDecryptTool defaultDecryptTool] decodeWithsuperFilePath:unzipPath headerCodeLength:downloadInfo.courseSecurityInfo.header keyCodeValue:downloadInfo.courseSecurityInfo.key progress:^(float progress) {
                        
                        // 解密回调
                        decryptProgress(progress);
                        
                    } complition:^(NSError *error) {
                        // 解密回调
                        
                        if (error) {
                            decryptProgress(-1.0f);
                            [mgr removeItemAtPath:zipPath error:nil];
                            [mgr removeItemAtPath:unzipPath error:nil];
                            
                        }else{
                            decryptProgress(1.0f);
                        }
                    }];
                    
                }else{
                    decryptProgress(1.0f);
                }
            }
        }];
        
    });
}
@end
